package GUI;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import ControlLayer.ForestillingsCtr;
import ModelLayer.Film;
import ModelLayer.Forestilling;

public class OpretForestilling {
	
	private ForestillingsCtr fctr;
	private JFrame frame;
	private JTextField fieldSal;
	private JTextField fieldDato;
	private JTextField fieldStart;
	private JComboBox<String> comboBox;
	private ArrayList<Film> flist;
	private ArrayList<Forestilling> forestillinger;
	private Film film;
	private JLabel lblSlut;
	private boolean result = false;
	private DateFormat dateFormat;
	private Date date;
	/**
	 * Create the application.
	 */
	 // Adds Forestillinger from database to the local arraylist forestillinger.  
	public OpretForestilling() {
		fctr = new ForestillingsCtr();
		forestillinger = fctr.getAllForestilling();
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		date = new Date();
		initialize();
		frame.setVisible(true);
		
	}
	// Initialize the GUI
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 12));
		frame.setBounds(100, 100, 255, 289);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);	
		frame.setResizable(false);
		

		JButton btnOpret = new JButton("Opret");
		btnOpret.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				String filmtitel = comboBox.getSelectedItem().toString(); 
		        byte b = Byte.valueOf(fieldSal.getText());
		        if(conflict())
		        {
		        	JOptionPane.showMessageDialog(frame,
							"Forestillingen conflicter med en anden.", "Error Message",
							JOptionPane.ERROR_MESSAGE);
		        }
		        else
		        {
		        	fctr.createForestilling(0, fieldDato.getText(), fieldStart.getText(), lblSlut.getText(), fctr.findFilm(filmtitel), fctr.getSal(b));
		        	frame.dispose();
		        }
			}
		});
		btnOpret.setBounds(114, 212, 89, 23);
		frame.getContentPane().add(btnOpret);
		
		comboBox = new JComboBox<String>();
		fctr = new ForestillingsCtr();
		flist = fctr.getAllFilm();
		for(Film f: flist)
		{
			
			comboBox.addItem(f.getTitel().toString());
		}
		
		
		comboBox.setBounds(117, 42, 112, 20);
		frame.getContentPane().add(comboBox);
		
		textFields();
		labels();
		
		//comboBox.add(comp)
		
		//using Escape to close the window
		KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
		Action escapeAction = new AbstractAction() {
		    // close the frame when the user presses escape
		    public void actionPerformed(ActionEvent e) {
		    		      frame.dispose();
		    }
		}; 
		frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
		frame.getRootPane().getActionMap().put("ESCAPE", escapeAction);

	}
	private void labels()
	{
		JLabel lblNewLabel = new JLabel("Film:");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel.setBounds(32, 44, 46, 14);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Sal:");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_1.setBounds(32, 75, 46, 14);
		frame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Dato:");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_2.setBounds(32, 106, 46, 14);
		frame.getContentPane().add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Start:");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_3.setBounds(32, 137, 46, 14);
		frame.getContentPane().add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("Slut:");
		lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewLabel_4.setBounds(32, 168, 46, 14);
		frame.getContentPane().add(lblNewLabel_4);
		
		JLabel lblOpretForestilling = new JLabel("Opret Forestilling");
		lblOpretForestilling.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblOpretForestilling.setBounds(32, 11, 111, 14);
		frame.getContentPane().add(lblOpretForestilling);
		
		lblSlut = new JLabel("SLUT");
		lblSlut.setBounds(127, 169, 76, 14);
		frame.getContentPane().add(lblSlut);
		
		
	}
	private void textFields()
	{
		fieldSal = new JTextField();
		fieldSal.setBounds(117, 72, 86, 20);
		frame.getContentPane().add(fieldSal);
		fieldSal.setColumns(10);
		
		fieldDato = new JTextField(dateFormat.format(date).toString());
		fieldDato.setBounds(117, 103, 86, 20);
		frame.getContentPane().add(fieldDato);
		fieldDato.setColumns(10);
		
		fieldStart = new JTextField();
		fieldStart.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e) {
						String filmtitel = comboBox.getSelectedItem().toString(); 
				    	Film f = fctr.findFilm(filmtitel);
				    	String laengde = f.getLaengde();     
				        int timerTal = Integer.parseInt(laengde.substring(0,1));
				        int minutter = Integer.parseInt(laengde.substring(3,5));
				        int startTimer = Integer.parseInt(fieldStart.getText().substring(0,2));
				        int startMin = Integer.parseInt(fieldStart.getText().substring(3,5));
		        		int finalTimer = timerTal + startTimer;
		        		int finalMin = startMin + minutter;
		        		String finalmins = "";
		        		if(finalMin > 59)
		        		{
		        			finalTimer++;
		        			finalMin -= - 60;
		        		}
		 		        if(finalMin == 0)
		 		        {
		        			finalmins = "00";
		        		}
		        		else
		        		{
		        			finalmins = Integer.toString(finalMin);
		        		}
		        		if(finalMin < 10)
		        		{
		        			finalmins = "0" + Integer.toString(finalMin);
		        		}
		        		else
		        		{
		        			finalmins = Integer.toString(finalMin);
		        		}	
		        		if(finalTimer >= 24)
		    			{
		        			finalTimer -= 24;
		    			}
			        	lblSlut.setText(finalTimer + ":" + finalmins);
			}
		});
		fieldStart.setBounds(117, 134, 86, 20);
		frame.getContentPane().add(fieldStart);
		fieldStart.setColumns(10);
	}	
	
	private boolean conflict()
	{
		
        String start = fieldStart.getText();
        int alt_I_Minutter = (Integer.parseInt(start.substring(0,2))*60) + 
        					  Integer.parseInt(start.substring(3,5));
       
		for(Forestilling fa : forestillinger)
		{
			String slutFundet = fa.getSlut();
	        int slut_I_Minutter = (Integer.parseInt(slutFundet.substring(0,2))*60) + 
	        					   Integer.parseInt(slutFundet.substring(3,5)) + 30;
	        
	        int datoYear = Integer.parseInt(fieldDato.getText().substring(0, 4));
	        int datoHour = Integer.parseInt(fieldDato.getText().substring(5, 7));
	        int datoMin = Integer.parseInt(fieldDato.getText().substring(8));
	        
			int newYear = Integer.parseInt(fa.getFremvisningsDato().substring(0, 4));
			int newHour = Integer.parseInt(fa.getFremvisningsDato().substring(5, 7));
			int newMin = Integer.parseInt(fa.getFremvisningsDato().substring(8));
			
			int sal = fa.getSal().getSalNr();
			int salNr = Integer.parseInt(fieldSal.getText());
			
	        if(datoYear == newYear && datoHour == newHour && datoMin == newMin && sal == salNr)
	        {
	        	int min = (alt_I_Minutter - slut_I_Minutter);
	        	if(min > 0)   			
	        	{	
	        		result = false;
	        	}
	        	else if(min <= 0)
	        	{
	        		result = true;
	        	}	
	        }
		}
		return result;
	}
}
